/**
 * 
 */
package cn.edu.bjtu.entity.impl;

import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;

import cn.edu.bjtu.constant.Constant;
import cn.edu.bjtu.entity.Command;
import cn.edu.bjtu.entity.ExecuteContext;
import cn.edu.bjtu.entity.ExecuteResult;
import cn.edu.bjtu.entity.impl.local.TFIDFTransExecuteContext;

/**
 * 这个单独写一个类是因为直接往网页输出有点特殊。。。。以前老代码就是坑啊。如果不好好规划一下的话。
 * @author alex
 *
 */
public class TFIDFTransCommand implements Command{
	
	public TFIDFTransCommand() {
		
	}
	@Override
	public ExecuteResult execute(ExecuteContext ctx) {
		
		if (ctx instanceof TFIDFTransExecuteContext) {
			TFIDFTransExecuteContext tfidfec = (TFIDFTransExecuteContext) ctx;
			tfidfec.fire();
			
			Map<String,Object> map = new HashMap<>();
			boolean success = tfidfec.isSuccess();
			String msg = tfidfec.getTipMessage();
			String res = null;
			if(tfidfec.getCtx().getOs() instanceof ByteArrayOutputStream){
				res = ((ByteArrayOutputStream)tfidfec.getCtx().getOs()).toString();
			}
			map.put(Constant.COMMAND_RESULT, res);
			StdViewRenderExecuteResult svrer = new StdViewRenderExecuteResult(false, success, msg, map);
			return svrer;
		}else{
			return new FailedExecuteResult("Invalid Execute Context");
		}

	}

}
